// pages/goodsList/goodsList.js
const app = getApp()
var api = require('../../../utils/api.js')
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    product: [],
    page: 1,
    limit: 12,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.index==1){
      this.setData({
        isShop: true,
        index: options.index
      })
    } else if (options.index == 2){
      this.setData({
        isShop: false,
        index: options.index
      })
    }
    if (options.shopId) {
      this.setData({
        shop_id: options.shopId
      })
    }

    if (options.q){
      var shop_id = options.q;
      var shop_id = unescape(decodeURI(shop_id));
      var shop_id = shop_id.substring(shop_id.lastIndexOf("=") + 1);
      this.setData({
        shop_id: shop_id,
        isShop: false
      })
    }
    this.goodsList();
  },
  onShow:function(){
    this.setData({
      url: app.globalData.base_url +'images/shore_blank.png'
    })
  },
goodsList: function () {
    var params = new Object();
    var that = this;
    params.limit = this.data.limit;
    params.page = this.data.page;
    if (this.data.shop_id){
      params.shop_id = this.data.shop_id;
    }
    // console.log(params)
    var that = this;
    api.POST({
      params: params,
      url: app.globalData.base_url + 'shop/getGoodsList',
      success: function (res) {
        wx.hideLoading()
        if (util.isEmpty(res.data.code)) {
          var paging = res.data.data.search;
          var totalPage = paging.total_page;
          var resultList = res.data.data.goods_list;
          var shopName = res.data.data.shop_info.shop_name;
          if (that.data.page == 1) {
            var itemData = resultList;
          } else {
            var itemData = that.data.product.concat(resultList)
          }
          that.setData({
            total_page: totalPage,
            product: itemData,
            shopName: shopName
          })

        } else {
          util.showError(res.data.message);
        }
      },
      fail: function (fail) {
        wx.hideLoading()
        util.showError("网络错误，请检查网络");

      }
    })
  },
//刷新页面
resetData: function () {
  var that = this
  that.setData({
    page: 1,
    hasMore: true
  });
},
//页面下拉刷新
onPullDownRefresh: function () {
  var that = this;

  setTimeout(function () {
    that.resetData()
  }, 1000)
},

//页面上拉触底
onReachBottom: function (e) {
  this.loadMore();
},
//加载更多
loadMore: function () {
  if (this.data.page >= this.data.total_page) {
    return;
  }
  wx.showLoading({
    title: '加载中',
  })
  this.setData({
    page: this.data.page + 1
  });
  this.goodsList();
},

//查看商品详情
productDetails: function (e) {
  var sku_code = e.currentTarget.dataset.sku_code;
  var curTime = e.timeStamp;
  var lastTime = this.data.lastTime;
  this.setData({
    lastTime: curTime
  })
  if (lastTime > 0) {
    if (curTime - lastTime < 500) {
      return false;
    }
  }
  if (this.data.isShop==false){
    wx.navigateTo({
      url: 'index/pages/productDetails/productDetails?sku_code=' + sku_code + '&shop_id=' + this.data.shop_id
    })
  }
},
})